import { setGlobals } from '@swda/utils';

/*:
* @plugindesc 枫之舞界面 - 战斗胜利
* @author 老饕
* @version 1.0.0
* @date 2020.5.11
*/

let SWDA_Victory = {};
// ========================================================
// BattleManager：处理战斗胜利
// ========================================================

SWDA_Victory.BattleManager_initMembers = BattleManager.initMembers;
BattleManager.initMembers = function () {
    SWDA_Victory.BattleManager_initMembers.call(this);
    this.initVictoryData();
};

BattleManager.initVictoryData = function () {
    this._victoryPhase = false;
};

BattleManager.processVictory = function () {
    $gameParty.removeBattleStates();
    $gameParty.performVictory();
    if (this.isVictoryPhase()) return;
    this._victoryPhase = true;
    AudioManager.stopBgm();
    this.playVictoryMe();
    this.makeRewards();
    this.gainRewards();
};


// 胜利阶段标志
BattleManager.isVictoryPhase = function () {
    return this._victoryPhase;
};

// 战斗胜利结束
BattleManager.processVictoryFinish = function () {
    this.endBattle(0);
    this.replayBgmAndBgs();
    this._victoryPhase = false;
};

// ========================================================
// Window_Victory 升级窗口
// ========================================================
function Window_Victory() {
    this.initialize.apply(this, arguments);
}

Window_Victory.prototype = Object.create(Window_Base.prototype);
Window_Victory.prototype.constructor = Window_Victory;


Window_Victory.prototype.initialize = function () {
    var width = this.windowWidth();
    var height = this.windowHeight();
    var x = (Graphics.boxWidth - width) / 2;
    var y = (Graphics.boxHeight - height) / 2;
    Window_Base.prototype.initialize.call(this, x, y, width, height);
    this.refresh();
    this.openness = 0;
};

Window_Victory.prototype.windowWidth = function () {
    return 384;
};

Window_Victory.prototype.windowHeight = function () {
    var items = BattleManager._rewards.items;
    if (items.length > 0)
        return this.fittingHeight(4 + items.length) + 8;
    else
        return this.fittingHeight(3) + 8;
};

Window_Victory.prototype.refresh = function () {
    this.contents.clear();

    var lineHeight = this.lineHeight();
    var gold = BattleManager._rewards.gold;
    var exp = BattleManager._rewards.exp;
    var items = BattleManager._rewards.items;

    var textVictory = TextManager.victory.format($gameParty.name());
    var textGold = TextManager.obtainGold.format(gold);
    var textExp = TextManager.obtainExp.format(exp, TextManager.exp);

    this.changeTextColor(this.systemColor());
    this.setTitleFont();
    this.drawText(textVictory, this.textPadding(), 0, this.contents.width - this.textPadding() * 2, 'center');
    this.resetFontSettings();
    this.drawHorzLine(lineHeight + 4);
    this.setSmallerFont();
    this.drawTextEx(textGold, this.textPadding(), lineHeight + 8, this.contents.width - this.textPadding() * 2);
    this.drawTextEx(textExp, this.textPadding(), lineHeight * 2 + 8, this.contents.width - this.textPadding() * 2);
    this.resetFontSettings();

    if (items.length > 0) {
        var textItem = TextManager.obtainItem.format('');
        this.setSmallerFont();
        this.drawText(textItem, this.textPadding(), this.lineHeight() * 3 + 8);
        var line = 4;
        var iconBoxWidth = Window_Base._iconWidth + 4;
        for (var i = 0; i < items.length; i++) {
            var item = items[i];
            this.drawIcon(item.iconIndex, this.textPadding() + iconBoxWidth + 2, lineHeight * line + 8);
            this.setSmallerFont();
            this.drawText(item.name, this.textPadding() + iconBoxWidth * 2, lineHeight * line + 8, this.contents.width - iconBoxWidth);
            line += 1;
        };
        this.resetFontSettings();
    }
};

Window_Victory.prototype.drawHorzLine = function (y) {
    var lineY = y - 1;
    this.contents.paintOpacity = 48;
    this.contents.fillRect(0, lineY, this.contentsWidth(), 2, this.lineColor());
    this.contents.paintOpacity = 255;
};

Window_Victory.prototype.lineColor = function () {
    return this.normalColor();
};

Window_Victory.prototype.drawTextEx = function(text, x, y) {
    if (text) {
      //this.resetFontSettings();
      var textState = { index: 0, x: x, y: y, left: x };
      textState.text = this.convertEscapeCharacters(text);
      textState.height = this.calcTextHeight(textState, false);
      while (textState.index < textState.text.length) {
        this.processCharacter(textState);
      }
      return textState.x - x;
    } else {
      return 0;
    }
  };
  

// ========================================================
// Window_LevelUp 战斗胜利窗口
// ========================================================


function Window_LevelUp() {
    this.initialize.apply(this, arguments);
}

Window_LevelUp.prototype = Object.create(Window_Base.prototype);
Window_LevelUp.prototype.constructor = Window_LevelUp;


Window_LevelUp.prototype.initialize = function () {
    var width = this.windowWidth();
    var height = this.windowHeight();
    var x = (Graphics.boxWidth - width) / 2;
    var y = (Graphics.boxHeight - height) / 2;
    Window_Base.prototype.initialize.call(this, x, y, width, height);
    this.refresh();
    this.openness = 0;
};

Window_LevelUp.prototype.windowWidth = function () {
    return 384;
};

Window_LevelUp.prototype.windowHeight = function () {
    var items = BattleManager._rewards.items;
    if (items.length > 0)
        return this.fittingHeight(4 + items.length);
    else
        return this.fittingHeight(3);
};

Window_LevelUp.prototype.refresh = function () {
    this.contents.clear();

    var lineHeight = this.lineHeight();
    var gold = BattleManager._rewards.gold;
    var exp = BattleManager._rewards.exp;
    var items = BattleManager._rewards.items;

    var textVictory = TextManager.victory.format($gameParty.name());
    var textGold = TextManager.obtainGold.format(gold);
    var textExp = TextManager.obtainExp.format(exp, TextManager.exp);

    this.drawText(textVictory, 0, 0, this.contents.width, 'center');
    this.drawHorzLine(lineHeight);
    this.drawTextEx(textGold, 0, lineHeight, this.contents.width);
    this.drawTextEx(textExp, 0, lineHeight * 2, this.contents.width);

    if (items.length > 0) {
        var textItem = TextManager.obtainItem.format('');
        this.drawText(textItem, 0, this.lineHeight() * 3);
        var line = 4;
        var iconBoxWidth = Window_Base._iconWidth + 4;
        for (var i = 0; i < items.length; i++) {
            var item = items[i];
            this.drawIcon(item.iconIndex, iconBoxWidth + 2, lineHeight * line);
            this.drawText(item.name, iconBoxWidth * 2, lineHeight * line, this.contents.width - iconBoxWidth);
            line += 1;
        };
    }

};

Window_LevelUp.prototype.drawHorzLine = function (y) {
    var lineY = y - 1;
    this.contents.paintOpacity = 48;
    this.contents.fillRect(0, lineY, this.contentsWidth(), 2, this.lineColor());
    this.contents.paintOpacity = 255;
};

Window_LevelUp.prototype.lineColor = function () {
    return this.normalColor();
};
//=============================================================================
// Scene_Battle
//=============================================================================
SWDA_Victory.Scene_Battle_update = Scene_Battle.prototype.update;
Scene_Battle.prototype.update = function () {
    SWDA_Victory.Scene_Battle_update.call(this);
    this.updateVictory();
};

Scene_Battle.prototype.updateVictory = function () {
    if (!BattleManager.isVictoryPhase()) return;
    this.updateVictorySteps();
};

Scene_Battle.prototype.updateVictorySteps = function () {
    if (!this._victoryWindow) {
        this._victoryWindow = new Window_Victory();
        this.addWindow(this._victoryWindow);
        this._victoryWindow.open();
    } else if (this.victoryTriggerContinue()) {
        this.finishVictory();
    }
};

Scene_Battle.prototype.finishVictory = function () {
    SoundManager.playOk();
    this._victoryWindow.close();
    this.processVictoryFinish();
};
Scene_Battle.prototype.processVictoryFinish = function () {
    BattleManager.processVictoryFinish();
};

Scene_Battle.prototype.victoryTriggerContinue = function () {
    if (Input.isRepeated('ok') || TouchInput.isRepeated()) return true;
    if (Input.isRepeated('cancel')) return true;
    return false;
};

setGlobals([
    ['Window_Victory',Window_Victory],
    ['Window_LevelUp',Window_LevelUp],
])